<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/5/31
 * Time: 14:08
 */

namespace Home\Controller;



use Home\Logic\OrderLogic;
use Home\Logic\UserLogic;
use Home\Logic\MoneyLogLogic;

class OrderController extends BaseController
{
    public function index(){

        $type = I('type');
        $logic = new OrderLogic();
        $allOrderList = $logic->getOrderList(0,true);
        $payOrderList = $logic->getOrderListByType($allOrderList,1);
        $shipmentOrderList = $logic->getOrderListByType($allOrderList,2);
        $receivedOrderList = $logic->getOrderListByType($allOrderList,3);
        $evaluationOrderList = $logic->getOrderListByType($allOrderList,4);
        $this->assign('allOrderList',$allOrderList);
        $this->assign('payOrderList',$payOrderList);
        $this->assign('shipmentOrderList',$shipmentOrderList);
        $this->assign('receivedOrderList',$receivedOrderList);
        $this->assign('evaluationOrderList',$evaluationOrderList);
        $this->assign('type',$type);

        $this->display();
    }

    /**
     * 订单详情
     */
    public function detail(){
        $order_id = I('order_id');
        $logic = new OrderLogic();
        $order = $logic->getOrderById($order_id);
        $result = $logic->getLogisticsInfo($order_id);
//        var_dump($result);
        $this->assign('logisticsInfo',$result['Traces']);
        $this->assign('order',$order);
        $this->display();
    }

    /**
     * 物流详情
     */
    public function logisticDetail(){
        $order_id = I('order_id');
        $logic = new OrderLogic();
        $order = $logic->getOrderById($order_id);
        $result = $logic->getLogisticsInfo($order_id);
//        var_dump($result);
        $this->assign('logisticsInfo',$result);
        $this->assign('order',$order);
        $this->assign('delivery',$result['delivery']);
        $this->display('logistics');
    }

    /**
     * 查看退换货列表
     */
    public function change(){
        $logic = new OrderLogic();
        $user = $this->getUser();
        $refundList = $logic->getReturnGoodsList(1,$user['user_id']);
        $exchangeList = $logic->getReturnGoodsList(0,$user['user_id']);
        $this->assign('refundList',$refundList);
        $this->assign('exchangeList',$exchangeList);
        $this->display();
    }

    /**
     *  跳转到退货申请
     */
    public function goToReturnGoods(){
        $rec_id = I('rec_id');
        $this->assign('rec_id',$rec_id);
        $this->display('add_return_goods');
    }

    /***
     * ajax退货
     */
    public function returnGoods(){
        $type = I('type');
        $content = I('content');
        $rec_id = I('rec_id');
        if(empty($content)){
            $this->ajaxReturn(array('success'=>false,'message'=>'退货原因不能为空!'));
        }
        $logic = new OrderLogic();
        $status = $logic->addReturnGoods($rec_id,$content);
        switch ($status){
            case 0:
                $this->ajaxReturn(array('success'=>false,'message'=>'提交失败！'));
                break;
            case 1:
                $this->ajaxReturn(array('success'=>true,'message'=>'提交成功！'));
                break;
            case 2:
                $this->ajaxReturn(array('success'=>false,'message'=>'该商品已经进行退货！','is_complete'=>false));
                break;
            case 3:
                $this->ajaxReturn(array('success'=>true,'message'=>'该商品不存在！','is_complete'=>false));
                break;
        }

    }

    /***
     * ajax标记删除订单
     */
    public function deleteOrder(){
        $order_id = I('order_id');
        $logic = new OrderLogic();
        $status = $logic->deleteOrder($order_id);
        if($status == 1){
            $this->ajaxReturn(array('success'=>true,'message'=>'删除成功！'));
        }else{
            $this->ajaxReturn(array('success'=>true,'message'=>'删除失败！'));
        }
    }

    /**
     * ajax确认收货
     */
    public function confirmReceipt(){
        $order_id = I('order_id');
        $logic = new OrderLogic();
        $userLogic = new UserLogic();
        $moneyLogic = new MoneyLogLogic();
        $status = $logic->confirmReceipt($order_id);
        if($status == 1){
//            $userLogic->rebate($order_id);
            //添加到消费记录中
            $moneyLogic->addAgentLog($order_id);
            $moneyLogic->rebateAgentLog($order_id);
            $this->ajaxReturn(array('success'=>true,'message'=>'已经确认收货！'));
        }else if($status == 2){
            $this->ajaxReturn(array('success'=>false,'message'=>'对不起，您不能进行该操作！'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'确认收货失败！'));
        }
    }

    public function test(){
        $goods = D('OrderGoods')->where(array('order_id' => 10))->find();
        $goods['goods_id'] = 3;
        unset($goods['rec_id']);
        D('OrderGoods')->add($goods);
    }

    /**
     * 跳转到评论界面
     */
    public function goToComment(){
        $order_id = I('order_id');
        $logic = new OrderLogic();
        $order = $logic->getOrderById($order_id);
        $this->assign('order',$order);
        $this->display('comment');
    }

    /**
     * ajax添加评论
     */
    public function addComment(){
        $order_id = I('order_id');
//        $evaluates = I('evaluates');
//        $contents = I('contents');
//        $goods_ids = I('goods_ids');
//        $rec_ids = I('rec_ids');
        $logic = new OrderLogic();
        $order = $logic->getOrderById($order_id);
        if(!$order){
            $this->ajaxReturn(array('success'=>false,'message'=>'该订单不存在'));
        }
//        $commentList = array();
//        for($i=0;$i<$order['goods_count'];$i++){
//            $commentList[$i]['content'] = $contents[$i];
//            $commentList[$i]['evaluate'] = $evaluates[$i];
//            $commentList[$i]['goods_id'] = $goods_ids[$i];
//            $commentList[$i]['rec_id'] = $rec_ids[$i];
//        }
        $commentList = session('commentList');
        $user = $this->getUser();
        $model = M();
        $model->startTrans();
        $status = $logic->replyComment($commentList,$order_id,$model,$user);
        $model->commit();

        switch ($status){
            case 0:
                $this->ajaxReturn(array('success'=>false,'message'=>'该订单不存在！'));
                break;
            case 1:
                $this->ajaxReturn(array('success'=>true,'message'=>'添加评论成功！'));
                break;
            case 3:
                $this->ajaxReturn(array('success'=>false,'message'=>'添加评论失败！'));
                break;
        }
    }

    /**
     * ajax提交单个评论 存在数据库
     */
    public function publishComment(){
        $order_id = I('order_id');
        $logic = new OrderLogic();
        $order = $logic->getOrderById($order_id);
        $user = $this->getUser();
        if(empty($user['user_id'])){
            $this->ajaxReturn(array('success'=>false,'message'=>'登录超时，请重新刷新界面！'));
        }
        if(!$order){
            $this->ajaxReturn(array('success'=>false,'message'=>'该订单不存在'));
        }
        $comment['type'] = I('evaluate');
        $comment['content'] = I('content');
        $comment['goods_id'] = I('goods_id');
        $rec_id = I('rec_id');
        if($comment['type']==''){
            $this->ajaxReturn(array('success'=>false,'message'=>'评价不能为空！'));
        }
        if(empty($comment['content'])){
            $this->ajaxReturn(array('success'=>false,'message'=>'评论内容不能为空！'));
        }
        if(empty($comment['goods_id']) || empty($rec_id)){
            return;
        }
        $orderGoods = D('OrderGoods')->where(array('rec_id'=>$rec_id))->find();
        if(!$orderGoods){
            $this->ajaxReturn(array('success'=>false,'message'=>'该订单不存在！','is_complete'=>false));
        }
        $isComplete = $logic->isCompleteComment($order_id);
        if($isComplete){
            $this->ajaxReturn(array('success'=>false,'message'=>'该订单所有商品已经评论完毕！'));
        }
        $is_comment = $logic->isCompleteCommentByOrderGoods($rec_id);
        if($is_comment){
            $this->ajaxReturn(array('success'=>false,'message'=>'该商品已经评论完毕！'));
        }
        $obj = $this->getUploadObj('comment');
        if($_FILES['cover']['error'][0] == 0) {
            $upload = $obj['upload'];
            $result = $upload->upload();

            if (!$result) {// 上传错误提示错误信息
                $this->ajaxReturn(array('success' => false, 'message' => '上传失败' . $upload->getError(), 'info' => $_POST));
            } else {// 上传成功
                $url = array();
                foreach ($result as $in) {//获取所有上传文件路径
                    $url[] = $obj['path'] . $in['savepath'] . $in['savename'];
                }
                $comment['image_urls'] = $url;
            }
        }


        $status = $logic->replyComment($comment,$order_id,$user,$rec_id);
        switch ($status){
            case 0:
                $this->ajaxReturn(array('success'=>false,'message'=>'该订单不存在！','is_complete'=>false));
                break;
            case 1:
                $allOrderList = $logic->getOrderList(0,true);
                $evaluationOrderList = $logic->getOrderListByType($allOrderList,4);
                $this->ajaxReturn(array('success'=>true,'message'=>'添加评论成功！','is_complete'=>!count($evaluationOrderList)));
                break;
            case 2:
                $this->ajaxReturn(array('success'=>false,'message'=>'添加评论失败！','is_complete'=>false));
                break;
            case 3:
                $this->ajaxReturn(array('success'=>true,'message'=>'保存成功！','is_complete'=>false));
                break;
        }
    }

    /**
     *评论界面
     */
    public function commentUI(){
        $order_id = I('order_id');
        $rec_id = I('rec_id');
        $logic = new OrderLogic();
        $allOrderList = $logic->getOrderList(0,true);
        $evaluationOrderList = $logic->getOrderListByType($allOrderList,4);
//        var_dump(count($evaluationOrderList));
//        $order = $logic->getOrderById($order_id);

//        $goodsList = $order['goodsList'];
//        $goodsList = $logic->getNoCommentOrderGoodsById($order_id);
        if(empty($evaluationOrderList)){
            return;
        }else if(count($evaluationOrderList) != 1){
            $this->assign('is_complete',0);
        }else{
            $this->assign('is_complete',1);
        }

        $goods = M('order_goods')->where(array('rec_id'=> $rec_id))->find();
//        $this->assign('goods',$goodsList[0]);
        $this->assign('goods', $goods);
        $this->display('publish_comment');
    }

    public function dispatchOrder(){
        $this->display();
    }

    /**
     * ajax获取配送员订单列表
     */
    public function ajaxDispatchOrder(){
        $user = $this->getUser();
        $pagenum = I('pagenum',1);
        $logic = new OrderLogic();
        $data = $logic->dispatchOrder($user['user_id'],$pagenum,10);
        $this->assign('data',$data);
        $this->display();
    }

    //ajax确认免费领取订单
    public function ajaxTakeSpecGoods(){

        $user = $this->getUser();
        $takSpecGoodsId = I('id');
        $type = I('type');
        $orderLogic = new OrderLogic();


        $re = $orderLogic->confirmCancelTakeSpecGoods($user['user_id'], $takSpecGoodsId,$type);

        $this->ajaxReturn($re);
    }

    //获取免费订单
    public function getFreeOrderList(){
        $user = $this->getUser();
        $logic = new OrderLogic();
        $orderList = $logic->getFreeOrder($user['user_id']);
//        var_dump($orderList);
        $this->assign('freeOrderList', $orderList);
        $this->assign('order_count',count($orderList));
        $this->display('freeorder');

    }
    //生成退款请求
    public function applyRefund(){
        $orderId = I('order_id');
        if(!$orderId){
            $this->ajaxReturn(array('status'=> -1, 'msg'=> '订单ID为空'));
        }
        $user = $this->getUser();
        $logic = new OrderLogic();
        $result = $logic->applyRefund($orderId, $user['user_id']);
        $this->ajaxReturn($result);
    }


    /**
     * 获取用户退款处理页面
     */
    public function refundList(){

        $user = $this->getUser();
        $refundList = M('refund')->where(array('user_id' =>$user['user_id']))->select();
        $finish = array();
        foreach ($refundList as $key => $val){
            $order = M('order')->where(array('order_sn'=> $val['order_sn']))->find();
            $val['goodsList'] = M('OrderGoods')->where(array('order_id'=> $order['order_id']))->select();
            $val['goods_count'] = count( $val['goodsList']);
            $val['order_amount'] = $order['order_amount'];
            if($val['status'] == 1)
                $finish[] = $val;
            else
                $noFinish[] = $val;
        }
//        var_dump($noFinish);
        $this->assign('noFinish', $noFinish);
        $this->assign('finish', $finish);
        $this->display('refund');
    }

}